package com.kevin.swing.impl;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;

import com.kevin.MainFrame;
import com.kevin.model.db.DBTableModel;
import com.kevin.model.jtable.JTableModel;
import com.kevin.swing.ContentJPanel;
import com.kevin.util.JTableUtil;

public class ContentStep2JPanel extends ContentJPanel {

    private static final long serialVersionUID = 4865803795120029389L;

    private int index = 0;

    private JLabel searchKeyLabel = new JLabel("关键字：");
    private JTextField searchText = new JTextField();// 搜素关键字

    private int searchNum = 0; // 搜索条目

    public ContentStep2JPanel(MainFrame owner) {
        super(owner);
        init();
    }

    protected void init() {
        setPanel();
    }

    protected void setPanel() {
        logTA = new JTextArea();
        logTA.setLineWrap(true);
        logTA.setBorder(null);
        jsplogTA = new JScrollPane(logTA);
        initJTable();
        setTableColumWidth();

        showtableJSpane = new JScrollPane(showTable);
        showtableJSpane.setPreferredSize(new Dimension(600, 69));

        // 搜索触发事件
        addSearchTextListener();

        JPanel searchbtnPanel = new JPanel();
        searchbtnPanel.setLayout(new GridBagLayout());
        searchbtnPanel.add(searchKeyLabel, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
                new Insets(0, 0, 0, 0), 0, 0));
        searchbtnPanel.add(searchText, new GridBagConstraints(4, 0, 2, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
                new Insets(0, 0, 0, 0), 0, 0));
        JPanel searchPanel = new JPanel();
        searchbtnPanel.add(searchPanel, new GridBagConstraints(6, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
                new Insets(0, 0, 0, 0), 0, 0));

        setLayout(new BorderLayout());
        add(searchbtnPanel, BorderLayout.NORTH);
        add(showtableJSpane, BorderLayout.CENTER);
        add(jsplogTA, BorderLayout.SOUTH);

    }

    /**
     * 搜索时重置表格内容
     * 
     * @Title resetSearchTable
     * @author 吕凯
     * @date 2016年8月23日 下午3:56:29 void
     */
    private void resetSearchTable() {
        index = 0;
        showTableModel = new DefaultTableModel(JTableModel.DEFAULTINFO_STEP2, JTableModel.COLNAMES_STEP2);
        showTable.setModel(showTableModel);
        setTableColumWidth();
    }

    /**
     * 搜索表返回内容容器jtable初始化
     * 
     * @Title initJTable
     * @author 吕凯
     * @date 2016年8月23日 下午3:23:27 void
     */
    private void initJTable() {
        showTableModel = new DefaultTableModel(JTableModel.DEFAULTINFO_STEP2, JTableModel.COLNAMES_STEP2);
        showTable = new JTable(showTableModel) {
            private static final long serialVersionUID = 1234561111L;

            // 不可编辑
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }

        };
        showTable.setRowHeight(20);
    }

    /**
     * 搜索输入框输入内容变化的监听方法
     * 
     * @Title addSearchActionListener
     * @author 吕凯
     * @date 2016年8月23日 下午3:21:00 void
     */
    private void addSearchTextListener() {
        searchText.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                textChange(e);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                textChange(e);
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                textChange(e);

            }

            private void textChange(DocumentEvent e) {
                fillData();
            }
        });
    }

    // 填充数据
    public void fillData() {
        searchNum = 0;
        List<String> selectedTables = owner.getSelectedTables();
        if (!selectedTables.isEmpty()) {
            resetSearchTable();
            if (searchText.getText().length() == 0) {
                selectedTables.forEach(tableName -> {
                    DBTableModel tableShow = owner.getSearchTables().stream().filter(table -> table.getTableName().equals(tableName))
                            .findFirst().get();
                    addTableRow(tableShow);
                });
                showlog("选中" + selectedTables.size() + "个表");
            } else {
                String searchkey = searchText.getText().toLowerCase();
                selectedTables
                        .stream()
                        .filter(tableName -> tableName.toLowerCase().contains(searchkey))
                        .forEach(
                                tableName -> {
                                    searchNum++;
                                    DBTableModel tableShow = owner.getSearchTables().stream()
                                            .filter(table -> table.getTableName().equals(tableName)).findFirst().get();
                                    addTableRow(tableShow);
                                });
                if (searchNum == 0) {
                    owner.disenabledNextStep();
                } else {
                    owner.enabledNextStep();
                }
                showlog("搜索到表记录" + searchNum + "条");
            }
        }
    }

    /**
     * 设置表格宽度
     * 
     * @Title setTableColumWidth
     * @author 吕凯
     * @date 2016年8月23日 下午4:07:46 void
     */
    private void setTableColumWidth() {
        JTableUtil.fitTableColumns(showTable, new int[] { 30, 350, 120 });
    }

    public void addTableRow(DBTableModel table) {
        showTableModel.addRow(JTableModel.dbTable2jTableStep2(table, ++index));
//        defaultModel.addRow(new String[] { "" }); // 空行
    }

}
